package com.cypher.encryptionanddecryption.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.adapter.BluetoothListAdapter;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothUtilManager;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothManagerSingleton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 悬浮按钮实现
 * @author: 古霸天
 * @date: 2020/11/4
 */
public class FloatingActionSearchFragment extends Fragment {
    public Button searchBluetoothBtn;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_floatingactionbutton, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        searchBluetoothBtn = view.findViewById(R.id.search_bluetooth_btn);
        // 按钮初始化后进行旋转
        setBtnAnimation();
        // 按钮设置监听事件，开启蓝牙扫描
        View.OnClickListener searchBtnClick = (v) -> {
            setBtnAnimation();
            BluetoothUtilManager bluetoothUtilManager =  BluetoothManagerSingleton.getInstance().getBluetoothUtilManager();
            bluetoothUtilManager.startScanBluetoothDevice();
        };
        searchBluetoothBtn.setOnClickListener(searchBtnClick);
    }

    /**
     * 设置按钮动画效果
     */
    public void setBtnAnimation() {
        final RotateAnimation animation = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1000);
        animation.setRepeatCount(-1);
        animation.setInterpolator(new LinearInterpolator());
        searchBluetoothBtn.startAnimation(animation);
    }

    /**
     * 清除按钮动画效果
     */
    public void clearBtnAnimation() {
        searchBluetoothBtn.clearAnimation();
    }
}
